


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: PropertiesFileDataStore</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.schauer.gregor.vienna.datastore.file</a> ]
</div>

<h1>Coverage Summary for Class: PropertiesFileDataStore (at.schauer.gregor.vienna.datastore.file)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">PropertiesFileDataStore</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/ 1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (5/ 5)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    87.1%
  </span>
  <span class="absValue">
    (27/ 31)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2012 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.schauer.gregor.vienna.datastore.file;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import at.schauer.gregor.vienna.context.ViennaContext;
<i>19</i>&nbsp;import at.schauer.gregor.vienna.datastore.StorageMetadata;
<i>20</i>&nbsp;import at.schauer.gregor.vienna.exception.ViennaException;
<i>21</i>&nbsp;import at.schauer.gregor.vienna.serializer.Serializer;
<i>22</i>&nbsp;import org.apache.commons.collections.MapUtils;
<i>23</i>&nbsp;import org.apache.commons.io.IOUtils;
<i>24</i>&nbsp;import org.springframework.core.io.FileSystemResource;
<i>25</i>&nbsp;import org.springframework.core.io.support.PropertiesLoaderUtils;
<i>26</i>&nbsp;import org.springframework.util.DefaultPropertiesPersister;
<i>27</i>&nbsp;
<i>28</i>&nbsp;import java.io.File;
<i>29</i>&nbsp;import java.io.FileOutputStream;
<i>30</i>&nbsp;import java.io.IOException;
<i>31</i>&nbsp;import java.io.OutputStream;
<i>32</i>&nbsp;import java.util.Enumeration;
<i>33</i>&nbsp;import java.util.LinkedHashMap;
<i>34</i>&nbsp;import java.util.Map;
<i>35</i>&nbsp;import java.util.Properties;
<i>36</i>&nbsp;
<i>37</i>&nbsp;/**
<i>38</i>&nbsp; * @author Gregor Schauer
<i>39</i>&nbsp; */
<i>40</i>&nbsp;public class PropertiesFileDataStore&lt;T&gt; extends AbstractFileDataStore&lt;T&gt; {
<i>41</i>&nbsp;	public PropertiesFileDataStore(ViennaContext ctx, File rootDirectory) {
<b class="fc"><i>42</i>&nbsp;		this(ctx, rootDirectory, &quot;properties&quot;);</b>
<b class="fc"><i>43</i>&nbsp;	}</b>
<i>44</i>&nbsp;
<i>45</i>&nbsp;	public PropertiesFileDataStore(ViennaContext ctx, File rootDirectory, String ext) {
<b class="fc"><i>46</i>&nbsp;		super(ctx, rootDirectory, ext);</b>
<b class="fc"><i>47</i>&nbsp;	}</b>
<i>48</i>&nbsp;
<i>49</i>&nbsp;	@Override
<i>50</i>&nbsp;	public &lt;E extends T&gt; E read(File file, StorageMetadata&lt;E&gt; metadata) {
<b class="fc"><i>51</i>&nbsp;		Map&lt;String, Object&gt; map = null;</b>
<i>52</i>&nbsp;		try {
<b class="fc"><i>53</i>&nbsp;			Properties properties = PropertiesLoaderUtils.loadProperties(new FileSystemResource(file));</b>
<b class="fc"><i>54</i>&nbsp;			map = fromProperties(properties);</b>
<b class="fc"><i>55</i>&nbsp;			map.remove(Serializer.CLASS);</b>
<b class="nc"><i>56</i>&nbsp;		} catch (IOException e) {</b>
<b class="nc"><i>57</i>&nbsp;			throw new RuntimeException(e);</b>
<b class="fc"><i>58</i>&nbsp;		}</b>
<i>59</i>&nbsp;
<b class="fc"><i>60</i>&nbsp;		Serializer&lt;E&gt; serializer = ctx.getSerializer(metadata.type);</b>
<b class="fc"><i>61</i>&nbsp;		return serializer.load(map, metadata.type);</b>
<i>62</i>&nbsp;	}
<i>63</i>&nbsp;
<i>64</i>&nbsp;	@Override
<i>65</i>&nbsp;	public void write(File file, StorageMetadata&lt;T&gt; metadata) {
<b class="fc"><i>66</i>&nbsp;		Serializer&lt;T&gt; serializer = ctx.getSerializer(metadata.type);</b>
<b class="fc"><i>67</i>&nbsp;		Map&lt;String, Object&gt; map = serializer.save(metadata.object);</b>
<b class="fc"><i>68</i>&nbsp;		Properties properties = MapUtils.toProperties(map);</b>
<i>69</i>&nbsp;
<b class="fc"><i>70</i>&nbsp;		OutputStream writer = null;</b>
<i>71</i>&nbsp;		try {
<b class="fc"><i>72</i>&nbsp;			writer = new FileOutputStream(file);</b>
<b class="fc"><i>73</i>&nbsp;			DefaultPropertiesPersister persister = new DefaultPropertiesPersister();</b>
<b class="fc"><i>74</i>&nbsp;			persister.store(properties, writer, null);</b>
<b class="nc"><i>75</i>&nbsp;		} catch (IOException e) {</b>
<b class="nc"><i>76</i>&nbsp;			throw new ViennaException(e);</b>
<i>77</i>&nbsp;		} finally {
<b class="fc"><i>78</i>&nbsp;			IOUtils.closeQuietly(writer);</b>
<b class="fc"><i>79</i>&nbsp;		}</b>
<b class="fc"><i>80</i>&nbsp;	}</b>
<i>81</i>&nbsp;
<i>82</i>&nbsp;	public static Map&lt;String, Object&gt; fromProperties(Properties properties) {
<b class="fc"><i>83</i>&nbsp;		Map&lt;String, Object&gt; map = new LinkedHashMap&lt;String, Object&gt;();</b>
<b class="fc"><i>84</i>&nbsp;		for (Enumeration&lt;?&gt; e = properties.propertyNames(); e.hasMoreElements(); ) {</b>
<b class="fc"><i>85</i>&nbsp;			String key = (String) e.nextElement();</b>
<b class="fc"><i>86</i>&nbsp;			map.put(key, properties.getProperty(key));</b>
<b class="fc"><i>87</i>&nbsp;		}</b>
<b class="fc"><i>88</i>&nbsp;		return map;</b>
<i>89</i>&nbsp;	}
<i>90</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2012-07-17 20:22</div>
</div>
</body>
</html>
